package com.hackathon.sgstern;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.hackathon.sgstern.dataprovider.DataProvider;
import com.hackathon.sgstern.dataprovider.LogoProvider;
import com.hackathon.sgstern.joinevent.JoinEvent;
import com.hackathon.sgstern.model.SportEvent;

public class SurroundingArea extends Activity implements
		OnInfoWindowClickListener, OnMapLongClickListener {

	public static final String NEW_EVENT_POSITION_KEY = "newEventPosition";
	public static final String JOIN_EVENT_KEY = "joinEvent";
	private GoogleMap map;
	private Map<Marker, SportEvent> eventMarkers = new HashMap<Marker, SportEvent>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_surrounding_area);

		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();
		map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		map.setOnInfoWindowClickListener(this);
		map.setOnMapLongClickListener(this);

		drawSportEvents();

		// Show the Up button in the action bar.
		setupActionBar();

		Bundle extras = getIntent().getExtras();
		if (extras.containsKey(JoinEvent.CENTER_EVENT_INTENT)) {
			SportEvent focusEvent = (SportEvent) extras
					.get(JoinEvent.CENTER_EVENT_INTENT);

			map.moveCamera(CameraUpdateFactory.newLatLngZoom(
					focusEvent.getOrt(), 10));
			map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
		}
	}

	private void drawSportEvents() {
		DataProvider dataProvider = DataProvider.getInstance();
		List<SportEvent> events = dataProvider.getEvents();
		for (SportEvent event : events) {
			MarkerOptions eventOptions = createMarkerOptions(event);
			Marker marker = map.addMarker(eventOptions);
			eventMarkers.put(marker, event);
		}
	}

	private MarkerOptions createMarkerOptions(SportEvent event) {
		MarkerOptions eventOptions = new MarkerOptions().position(event
				.getOrt());
		eventOptions.icon(LogoProvider.retrieveBitmapDescriptor(event.art));
		eventOptions.title(event.title);
		return eventOptions;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.surrounding_area, menu);
		return true;
	}

	@Override
	public void onInfoWindowClick(Marker marker) {
		// Toast.makeText(this, "onInfoWindowClick", Toast.LENGTH_SHORT).show();
		Intent joinEvent = new Intent(this, JoinEvent.class);
		joinEvent.putExtra(JOIN_EVENT_KEY, eventMarkers.get(marker));
		startActivity(joinEvent);
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public void onMapLongClick(LatLng point) {
		Intent newEvent = new Intent(this, EventCreation.class);
		newEvent.putExtra(NEW_EVENT_POSITION_KEY, point);
		startActivity(newEvent);
	}

}
